package com.hzhk.happyrent.ui.activity.personal;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.person.AddressModel;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.activity.UpdateAddressActivity;
import com.hzhk.happyrent.ui.adapter.DataNullAdapter;
import com.hzhk.happyrent.ui.adapter.my.AddressAdapter;
import com.hzhk.happyrent.ui.view.listview.MaiTianListview;
import com.hzhk.happyrent.utils.ListUtils;
import com.hzhk.happyrent.utils.MyPerference;
import com.hzhk.happyrent.utils.ToastUtil;
import com.tamic.novate.Throwable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
* 收货地址管理界面
*
* */
public class AddressActivity extends Activity{


    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.activity_titlebar_right)
    ImageView activityTitlebarRight;
    @BindView(R.id.lv_fragment_card)
    ListView lvFragmentCard;
    @BindView(R.id.address_update)
    Button addressUpdate;
    private AddressAdapter addressAdapter;
    private Logger log = LoggerFactory.getLogger(this.getClass().getName());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initData();
    }

    private void initData() {
        String userId = MyPerference.getInstance().getUserId();
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        HttpManager.getInstance().perform_post(API.USER_ADDRESS, map, new MyStringResponse(this) {
            @Override
            public void onError(Object tag, Throwable e) {
//                Toast.makeText(AddressActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                log.debug("response = " + response);
                AddressModel addressModel = new Gson().fromJson(response, AddressModel.class);
                //CardHotModel  cardHotModel = new Gson().fromJson(response,CardHotModel.class);
                if(ListUtils.isEmpty(addressModel.getData().getUser_address())){
                    lvFragmentCard.setAdapter(new DataNullAdapter(AddressActivity.this));
                }else{
                    lvFragmentCard.setAdapter(addressAdapter);
                    addressAdapter.setList(addressModel.getData().getUser_address());
                }
            }
        });
    }

    private void initView(){
        log.debug("jump into initView");
        activityTitlebarLeft.setImageResource(R.drawable.back);
        activityTitlebarRight.setVisibility(View.GONE);
        if(!TextUtils.isEmpty(getIntent().getStringExtra("title"))){
            activityTitlebarCenter.setText(getIntent().getStringExtra("title"));
            lvFragmentCard.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    AddressModel.DataBean.UserAddressBean item = ((AddressAdapter) ((ListView) parent).getAdapter()).getItem(position);
                    getIntent().putExtra("address_name",item.getUser_name());
                    getIntent().putExtra("address_phone",item.getPhone());
                    getIntent().putExtra("address",item.getArea());
                    getIntent().putExtra("address_info",item.getAddress());
                    getIntent().putExtra("address_id",item.getId());
                    setResult(1,getIntent());
                    finish();
                }
            });
        }else{
            activityTitlebarCenter.setText(R.string.administer_address);
        }
        addressAdapter = new AddressAdapter(this);
    }
    @OnClick({R.id.activity_titlebar_left, R.id.address_update})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_titlebar_left:
                finish();
                break;
            case R.id.address_update:
                int count = addressAdapter.getCount();
                if(count > 10) {
                    Toast.makeText(this, R.string.max_address_num, Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(this, UpdateAddressActivity.class);
                startActivity(intent);
                break;
        }
    }


}
